package com.asset.dispoal.business.bsCase.service.commom;

import com.asset.dispoal.business.bsCase.domain.db.entity.BsCase;
import com.asset.dispoal.business.bsCase.domain.db.mapper.BsCaseMapper;
import com.asset.dispoal.business.bsCase.dto.BsCaseRequest;
import com.asset.dispoal.business.bsCase.dto.BsCaseResponse;
import com.asset.dispoal.business.bsCase.service.converter.BsCaseConverter;
import com.asset.dispoal.commom.page.PagingQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/4/12$
 */
@Component
public class QueryBorrowerCaseByPageCommand implements BaseCommand<BsCaseRequest, Result<PageVO<BsCaseResponse>>> {


    @Autowired
    BsCaseMapper bsCaseMapper;

    @Override
    public Result<PageVO<BsCaseResponse>> execute(BsCaseRequest bsCaseRequest) {
        BsCase bsCase = BsCaseConverter.INSTANCE.reqToDo(bsCaseRequest);
        String permission = (String) GlobalHeaderThreadLocal.get().getExtraMap().get("permission");
        bsCase.setPermission(permission);
        int count = bsCaseMapper.queryBorrowerCaseCount(bsCase);
        Page<BsCase> page = PagingQuery.getPage(count, bsCase);
        PageVO<BsCaseResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);

        if (page != null) {
            List<BsCase> bsCases = bsCaseMapper.queryBorrowerCaseByPage(page);
            List<BsCaseResponse> bsCaseResponses = BsCaseConverter.INSTANCE.dosToRes(bsCases);
            pageVO.setResult(bsCaseResponses);
        }
        return Result.ok(pageVO);
    }
}
